package com.estar.assistsurvy.adapter;

import java.util.List;

import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.estar.assistsurvy.R;
import com.estar.assistsurvy.entity.RequestGrab;
import com.estar.assistsurvy.entity.grabPondList;
import com.estar.assistsurvy.thread.GrabOrderThread;
import com.estar.assistsurvy.utils.MightypleUtil;
import com.estar.assistsurvy.utils.TimeUtils;
import com.estar.assistsurvy.widget.TimeTextView;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class grabPondAdapter extends BaseAdapter implements OnClickListener,OnGetGeoCoderResultListener{

	private List<grabPondList> orders;
	private grabPondList order;
	private LayoutInflater inflater;
	private Context context;
	private Handler handler;
	private String location,adress,Poi,longitude,latitude,curentTime;
	public grabPondAdapter(List<grabPondList> orders, Context context,Handler handler) {
		this.orders = orders;
		this.context = context;
		inflater=LayoutInflater.from(context);
		GeoCoder.newInstance();
		this.handler=handler;
		
	}
	
	public void setCurrentTime(String curentTime){
		this.curentTime=curentTime;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return orders.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return orders.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		viewHolder holder;
		if(convertView==null){
			convertView=inflater.inflate(R.layout.listview_order, null);
			holder=new viewHolder();
			holder.castTime=(TextView) convertView.findViewById(R.id.castTime);
			holder.order_address=(TextView) convertView.findViewById(R.id.order_address);
			holder.order_distance=(TextView) convertView.findViewById(R.id.order_distance);
			holder.order_leftTime= (TimeTextView) convertView.findViewById(R.id.order_leftTime);
			holder.order_carNumber=(TextView) convertView.findViewById(R.id.order_carNumber);
			holder.orderCharge=(TextView) convertView.findViewById(R.id.orderCharge);
			holder.robOrder=(ImageView) convertView.findViewById(R.id.robOrder);
			holder.order_gpsIco=(ImageView) convertView.findViewById(R.id.order_gpsIco);
			holder.order_timeIco=(ImageView) convertView.findViewById(R.id.order_timeIco);
			holder.order_carIco=(ImageView) convertView.findViewById(R.id.order_carIco);
			holder.orderDetail=(ImageView) convertView.findViewById(R.id.orderDetail);
			convertView.setTag(holder);
		}else{
			holder=(viewHolder) convertView.getTag();
		}

		order=orders.get(position);
		holder.castTime.setText(TimeUtils.EnglishToChinese(order.getUpdateTime()));
		holder.order_address.setText(order.getOccurPlace());
		holder.order_distance.setText("距您"+order.getDistance()+"km");
		Boolean flag=true;
		if(flag){
//			int time=Integer.parseInt(order.getLeftTime());
			long time=TimeUtils.getLeftTime(order.getUpdateTime(), curentTime, order.getLeftTime())/1000;
			int min=(int) (time/60);
			int sec=(int) (time%60);
//			int[] time_arry={0,0,min,sec,0};
			int[] time_arry={min,sec};
//			int[] time_arry={0,10};
			holder.order_leftTime.setTimes(time_arry);
			if (!holder.order_leftTime.isRun()) {
				holder.order_leftTime.run();
			}
			flag=false;
		}else{
			flag=true;
		}
		holder.order_carNumber.setText(order.getTargetCar());
		holder.order_gpsIco.setImageResource(R.drawable.icon_address2);
		holder.order_timeIco.setImageResource(R.drawable.icon_clock2);
		holder.order_carIco.setImageResource(R.drawable.icon_carnumber2);
		holder.orderDetail.setImageResource(R.drawable.btn_more);
		holder.robOrder.setImageResource(R.drawable.icon_scramble);
		holder.robOrder.setOnClickListener(this);
		holder.orderCharge.setText(order.getPredictCharge());
		
		
		return convertView;
	}

	private class viewHolder{
		private TextView castTime,order_address,order_distance,
		order_carNumber,orderCharge;
		private TimeTextView order_leftTime;
		private ImageView robOrder,order_gpsIco,order_timeIco,
		order_carIco,orderDetail;

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.robOrder:
			SharedPreferences share=context.getSharedPreferences("location", Context.MODE_PRIVATE);
			longitude=share.getString("longitude", "");
			latitude=share.getString("latitude", "");
			adress=share.getString("adress", "");
			Poi=share.getString("Poi", "");
			if(Poi.equals("")||null==Poi){
				location=adress;
			}else{
				location=Poi;
			}
			if(!latitude.equals("")&&!longitude.equals("")){
//				LatLng point = new LatLng(Float.parseFloat(latitude), Float.parseFloat(longitude));
//				mSearch.setOnGetGeoCodeResultListener(this);
//				mSearch.reverseGeoCode(new ReverseGeoCodeOption()  
//						.location(point));
				RequestGrab requestGrab=new RequestGrab(MightypleUtil.getDeviceId(context), 
						order.getUserId(), order.getCaseNumber(), order.getTaskNumber(), 
						TimeUtils.getLeftTime(order.getUpdateTime(), TimeUtils.getCurentTime(), order.getLeftTime())/1000+"", order.getPredictCharge(),order.getDistance(),
						longitude, latitude, 
						location);
//				RequestGrab requestGrab=new RequestGrab(MightypleUtil.getDeviceId(context), 
//						order.getUserId(), order.getCaseNumber(), order.getTaskNumber(), 
//						60+"", order.getPredictCharge(),order.getDistance(),
//						longitude, latitude, 
//						location);
				new GrabOrderThread(context, handler, requestGrab).start();
			}else{
				Toast.makeText(context, "定位失败，请稍后再试!", 0).show();
			}
			break;

		default:
			break;
		}
	}

	@Override
	public void onGetGeoCodeResult(GeoCodeResult arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
		// TODO Auto-generated method stub
//		location=result.getAddress();
//		RequestGrab requestGrab=new RequestGrab(MightypleUtil.getDeviceId(context), 
//				order.getUserId(), order.getCaseNumber(), order.getTaskNumber(), 
//				TimeUtils.getLeftTime(order.getUpdateTime(), TimeUtils.getCurentTime(), order.getLeftTime())/1000+"", order.getPredictCharge(),order.getDistance(),
//				longitude, latitude, 
//				location);
//		new GrabOrderThread(context, handler, requestGrab).start();
	}
}
